package com.xckj.cycling.fragment

import android.os.Bundle
import android.view.ViewGroup
import com.xckj.cycling.event.KeyboardEvent
import com.xckj.cycling.global.Constant
import com.xckj.cycling.utils.DensityUtil
import com.xckj.cycling.utils.LanguageUtils
import com.xckj.cycling.utils.SpUtil
import com.xckj.cycling.utils.StatusBarHelper
import com.xckj.cycling.view.CWebView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class CustomerServiceFragment : WebViewFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun initWebView(webView: CWebView?) {
        super.initWebView(webView)
        webView?.apply {
            loadUrl(
                Constant.WEB_URL_CUSTOMER + SpUtil.getString(
                    requireContext(),
                    Constant.TOKEN
                )+"&language=${LanguageUtils.getLanguage() }"
            )
            setPaddingRelative(
                0,
                StatusBarHelper.getStatusbarHeight(requireContext()),
                0,
                DensityUtil.dip2px(requireContext(), 60f)
            )
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }


    @Subscribe
    fun event(keyboardEvent: KeyboardEvent) {
        webView?.apply {
            setPaddingRelative(
                0,
                StatusBarHelper.getStatusbarHeight(requireContext()),
                0,
                if (keyboardEvent.height <= 0) DensityUtil.dip2px(
                    requireContext(),
                    60f
                ) else keyboardEvent.height
            )
        }
    }

}